﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Solution48
{
    public void Rotate1(int[][] matrix)//使用辅助数组
    {
        int m=matrix.Length;
        int[][] newArr = new int[m][];
        for (int i = 0; i < newArr.Length; i++)
        {
            newArr[i] = new int[m];
        }
        for (int i = 0; i < newArr.Length; i++)
        {


            for (int j = 0; j < newArr[i].Length; j++)
            {
                newArr[j][m - i - 1] = matrix[i][j];

            }

        }

        //matrix = newArr;

        for (int i = 0; i < newArr.Length; i++)
        {


            for (int j = 0; j < newArr[i].Length; j++)
            {

               matrix[i][j]= newArr[i][j];

            }

        }

    }
    //原地旋转
    public void Rotate2(int[][] matrix)
    {
        int m = matrix.Length;
      
        for (int i = 0; i <m/2; i++)
        {


            for (int j = 0; j < (m+1) / 2; j++)
            {
                int temp = matrix[j][m - i - 1];

                matrix[j][m - i - 1]= matrix[i][j];

                matrix[i][j] = matrix[m - j - 1][i];

                matrix[m - j - 1][i] = matrix[m - i - 1][m - j - 1];

                matrix[m - i - 1][m - j - 1]= temp;

            }

        }

      

    }



    public void Rotate(int[][] matrix)//先转置，再翻转
    {
        int m = matrix.Length;
        //翻转
        for (int i = 0; i < m / 2; i++)
        {


            for (int j = 0; j <m; j++)
            {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[m - i - 1][j] ;
                matrix[m-i - 1][j] = temp;

            }

        }
        //再转置

        for (int i = 0; i < m ; i++)
        {


            for (int j = 0; j < i; j++)
            {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;



            }

        }
    }
}


class Pragram48
{
    
    static void Main48(string[] args)
    {
        int m = 3;
        //Array arrs = new Array[10];//这句话生成的是有1个元素为10的数组

        int[][] arrs = new int[m][];
        for (int i = 0; i < arrs.Length; i++)
        {
            arrs[i]= new int[m];
            for (int j = 0; j < arrs[i].Length; j++)
            {
                arrs[i][j] = m*i+j+1;
                //Console.WriteLine(arrs[i][j]);


            }

        }


        //int[][] newArr = arrs;//这里是浅拷贝，原来的函数发生了变化



        Solution48 solution = new Solution48();
        solution.Rotate(arrs);


        for (int i = 0; i < arrs.Length; i++)
        {
            
            for (int j = 0; j < arrs[i].Length; j++)
            {
                Console.WriteLine(arrs[i][j]);


            }

        }

        Console.ReadKey();




    }

}
